package com.zzc.fwzpxt.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zzc.fwzpxt.entity.*;
import com.zzc.fwzpxt.service.ICompanyService;
import com.zzc.fwzpxt.service.IProductService;
import com.zzc.fwzpxt.service.IRegAddressService;
import com.zzc.common.controller.BaseController;
import com.zzc.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @User Administrator
 * @Date Create in 11:39/2017/10/6
 * @Descriptton {
 *     产品控制器
 * }
 **/
@Controller
public class BlogProductController extends BaseController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private ICompanyService iCompanyService;
    @Autowired
    private IRegAddressService iRegAddressService;

    @RequestMapping("/product.html")
    public String product(Model model,
                          @RequestParam(value="current", required=false, defaultValue="1") int current,
                          String search,
                          String companyType,
                          String regCapital,
                          String regYear,
                          String city,
                          String all, String orderby_regDate, String orderby_regCapital){
        model.addAttribute("search", search);
        Page<ProductVo> page = new Page<>(current, 10);
        EntityWrapper<ProductVo> productVoEntityWrapper = new EntityWrapper<>();
        productVoEntityWrapper.and("1=1");
        // 搜索条件
        if (StringUtil.isNotEmpty(search)) {
            productVoEntityWrapper.like("product_name", search.trim());
        }
        // 公司类型
        if (StringUtil.isNotEmpty(companyType)) {
            productVoEntityWrapper.eq("company_id", companyType);
        }
        // 注册资本
        if (StringUtil.isNotEmpty(regCapital)) {
            if ("1".equals(regCapital)) {
                // "注册资本 < 100"
                productVoEntityWrapper.lt("reg_capital", 100);
            } else if ("2".equals(regCapital)) {
                productVoEntityWrapper.ge("reg_capital", 100);
                productVoEntityWrapper.lt("reg_capital", 500);
            } else if ("3".equals(regCapital)) {
                productVoEntityWrapper.ge("reg_capital", 500);
                productVoEntityWrapper.lt("reg_capital", 1000);
            } else if ("4".equals(regCapital)) {
                productVoEntityWrapper.ge("reg_capital", 1000);
                productVoEntityWrapper.lt("reg_capital", 5000);
            } else if ("5".equals(regCapital)) {
                productVoEntityWrapper.ge("reg_capital", 5000);
            }
        }
        // 注册年限
        if (StringUtil.isNotEmpty(regYear)) {
            if ("1".equals(regYear)) {
                // "注册资本 < 100"
                productVoEntityWrapper.lt("reg_year", 1);
            } else if ("2".equals(regYear)) {
                productVoEntityWrapper.ge("reg_year", 1);
                productVoEntityWrapper.lt("reg_year", 3);
            } else if ("3".equals(regYear)) {
                productVoEntityWrapper.ge("reg_year", 3);
                productVoEntityWrapper.lt("reg_year", 5);
            } else if ("4".equals(regYear)) {
                productVoEntityWrapper.ge("reg_year", 5);
                productVoEntityWrapper.lt("reg_year", 10);
            } else if ("5".equals(regYear)) {
                productVoEntityWrapper.ge("reg_year", 10);
            }
        }
        // 注册地址
        if (StringUtil.isNotEmpty(city)) {
            productVoEntityWrapper.eq("reg_address_id", city);
        }
        // 排序
        if (StringUtil.isNotEmpty(orderby_regDate) || StringUtil.isNotEmpty(orderby_regCapital)) {
            // 注册时间
            if (StringUtil.isNotEmpty(orderby_regDate)) {
                productVoEntityWrapper.orderBy("reg_year", "up".equals(orderby_regDate)?true:false);
            }

            // 注册资本
            if (StringUtil.isNotEmpty(orderby_regCapital)) {
                productVoEntityWrapper.orderBy("reg_capital", "up".equals(orderby_regCapital)?true:false);
            }
        } else {
            productVoEntityWrapper.orderBy("reg_year", true);
        }
        productVoEntityWrapper.eq("product.status", 1);
        // 查询
        Page<ProductVo> products = iProductService.selectMyPage(page, productVoEntityWrapper);
        model.addAttribute("products", products);


/*        Page<Product> page = new Page<>(1, 5);
        EntityWrapper<Product> articleEntityWrapper = new EntityWrapper<>();
        articleEntityWrapper.eq("status",1);
        articleEntityWrapper.orderBy("gmt_create", false);
        Page<Product> productPage = iProductService.selectPage(page, articleEntityWrapper);
        model.addAttribute("productPage",productPage);*/

        //查询公司类型
        EntityWrapper<Company> companyEntityWrapper = new EntityWrapper<>();
        companyEntityWrapper.eq("status", 1);
        companyEntityWrapper.orderBy("sort", true);
        List<Company> companies = iCompanyService.selectList(companyEntityWrapper);
        model.addAttribute("companies", companies);

        //查询地址
        EntityWrapper<RegAddress> regAddressEntityWrapper = new EntityWrapper<>();
        regAddressEntityWrapper.eq("status", 1);
        regAddressEntityWrapper.orderBy("sort", true);
        List<RegAddress> regAddresses = iRegAddressService.selectList(regAddressEntityWrapper);
        model.addAttribute("regAddresses", regAddresses);
        return "/blog/product";
    }

    /**
     * 产品详情
     */
    @RequestMapping("/product/{id}")
    public String productDetail(Model model,@PathVariable Integer id){
        // 查询产品
        ProductVo productVo = iProductService.findById(id);
        model.addAttribute("product",productVo);
        // 随机查询4条产品
        List<Product> product4 = iProductService.findRandProduct4();
        model.addAttribute("product4",product4);
        return "/blog/product-content";
    }

}
